package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.Consts;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;


@Service
public class StrategyStatisVORedisServiceImpl implements IStrategyStatisVORedisService {
    @Autowired
    private StringRedisTemplate template;
    @Autowired
    private IStrategyService strategyService;

    @Override
    public void viewnumIncrease(String sid, int num) {

        StrategyStatisVO vo = this.getStrategyStatisVo(sid);
        //阅读数+num
        vo.setViewnum(vo.getViewnum() + num);
        this.setStrategyStatisVo(vo);

    }

    @Override
    public void increaseReplynum(String sid, int num) {
        StrategyStatisVO vo = this.getStrategyStatisVo(sid);
        //Replynum + num
        vo.setReplynum(vo.getReplynum() + num);
        this.setStrategyStatisVo(vo);
    }

    @Override
    public StrategyStatisVO getStrategyStatisVo(String sid) {
        String vokey = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        //判断vo是否存在
        StrategyStatisVO vo = null;

        if (!template.hasKey(vokey)) {
            //如果不存在,初始化
            vo = new StrategyStatisVO();
            Strategy strategy = strategyService.get(sid);

            BeanUtils.copyProperties(strategy, vo);
            vo.setStrategyId(strategy.getId());
            template.opsForValue().set(vokey, JSON.toJSONString(vo));
        } else {
            String voStr = template.opsForValue().get(vokey);
            vo = JSON.parseObject(voStr, StrategyStatisVO.class);
        }
        return vo;
    }

    @Override
    public void setStrategyStatisVo(StrategyStatisVO vo) {
        //1.拼接vokey
        String vokey = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        //更新
        template.opsForValue().set(vokey, JSON.toJSONString(vo));
    }

    @Override
    public List<String> getStrategiesFavor(String userId) {
        //拼接key
        String listkey = RedisKeys.USER_STRATEGY_FAVOR.join(userId);
        //判断list是否存在,如果存在获取 ,如果不存在直接new
        List<String> uidlist = new ArrayList<>();
        if (template.hasKey(listkey)) {
            String listStr = template.opsForValue().get(listkey);
            uidlist = JSON.parseArray(listStr, String.class);
        }
        return uidlist;
    }

    @Override
    public Boolean strategyThumbup(String sid, String uid) {
        //拼接标记的key
        String key = RedisKeys.STRATEGY_THUMB.join(uid, sid);
        //判断标记是否存在
        if (template.hasKey(key)) {
            //如果存在,直接返回false
            return false;
        }
        StrategyStatisVO vo = this.getStrategyStatisVo(sid);
        //如果不存在,点赞数+1,缓存标记,设置有效时间
        vo.setThumbsupnum(vo.getThumbsupnum() + 1);
        this.setStrategyStatisVo(vo);
        //有效时间
        Date date = new Date();

        Date endDate = DateUtil.getEndDate(date);
        long between = DateUtil.getDateBetween(date,endDate);

        //更新vo对象
        template.opsForValue().set(key, "1", between, TimeUnit.SECONDS);

        return true;
    }

    @Override
    public List<StrategyStatisVO> queryVoBypattern(String pattern) {
        ArrayList<StrategyStatisVO> list = new ArrayList<>();
        String p = RedisKeys.STRATEGY_STATIS_VO.join(pattern);
        Set<String> voKeys = template.keys(p);

        if (voKeys != null && voKeys.size() > 0) {
            for (String voKey : voKeys) {
                String voStr = template.opsForValue().get(voKey);
                list.add(JSON.parseObject(voStr, StrategyStatisVO.class));
            }
        }

        return list;
    }

    @Override
    public boolean isVoExists(String id) {
        String join = RedisKeys.STRATEGY_STATIS_VO.join(id);
        return template.hasKey(join);
    }


    @Override
    public Boolean favor(String sid, String uid) {
        //拼接key
        String listkey = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        //判断list是否存在,如果存在获取 ,如果不存在直接new
        List<String> uidlist = new ArrayList<>();
        if (template.hasKey(listkey)) {
            String listStr = template.opsForValue().get(listkey);
            uidlist = JSON.parseArray(listStr, String.class);
        }
        //判断sid是否list中
        StrategyStatisVO vo = this.getStrategyStatisVo(sid);
        if (uidlist.contains(sid)) {
            //在表示取消 ,收藏数-1 移除list
            vo.setFavornum(vo.getFavornum() - 1);
            uidlist.remove(sid);
        } else {
            //不在在表示收藏 ,收藏数+1 添加list
            vo.setFavornum(vo.getFavornum() + 1);
            uidlist.add(sid);
        }
        //更新list,vo对象
        template.opsForValue().set(listkey, JSON.toJSONString(uidlist));
        this.setStrategyStatisVo(vo);

        return uidlist.contains(sid);
    }

    /*@Override
    public boolean favor(String sid, String uid) {
        //拼接用户攻略收藏id集合的key
        String key = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        //获取redis上面用户攻略收藏id集合: sidList
        List<String> sidList = new ArrayList<>();
        if (template.hasKey(key)) {
            String sidListStr = template.opsForValue().get(key);
            //解析json集合, 参数1:集合json字符串, 参数2:集合中元素类型(泛型)
            sidList = JSON.parseArray(sidListStr, String.class);
        }
        boolean flag = false;
        StrategyStatisVO vo = this.getStrategyStatisVo(sid);
        if (sidList.contains(sid)) {
            //判断sid是否在sidList, 如果在表示取消收藏操作, 收藏数-1, 移除出sidList
            vo.setFavornum(vo.getFavornum() - 1);
            sidList.remove(sid);
        } else {
            //判断sid是否在sidList, 如果不在表示收藏操作, 收藏数+1, 添加sidList中
            vo.setFavornum(vo.getFavornum() + 1);
            sidList.add(sid);
            flag = true;
        }
        //更新list 更新vo
        template.opsForValue().set(key, JSON.toJSONString(sidList));
        this.setStrategyStatisVo(vo);
        //return flag;
        return sidList.contains(sid);
    }*/

}



